<?

class Group_model extends CI_Model
{	
	function __construct()
	{
		parent::__construct();
		
	}
	
	private function _required($required, $data)
	{
		foreach($required as $field) if(!isset($data[$field])) return false;
		return true;
	}
	
	private function _default($defaults, $options)
	{
		return array_merge($defaults, $options);
	}
	
	function insert($options = array())
	{
		if(!$this->_required(array('g_name'), $options)) 
			return false;
	
		//$options = $this->_default(array('u_created_on' => sql_current_datetime()), $options);
		$qualificationArray = array('g_name', 'g_description', 'p_cms_dashboard', 'p_cms_pages', 'p_cms_menus', 'p_cms_filemanager');
		foreach($qualificationArray as $qualifier)
			if(isset($options[$qualifier])) 
				$this->db->set($qualifier, $options[$qualifier]);
	 
		$this->db->insert('groups');
	
		return $this->db->insert_id();
	}
	
	function update($options = array())
	{
		if(!$this->_required(array('g_id'), $options)) 
			return false;
	
		$qualificationArray = array('g_name', 'g_description', 'p_cms_dashboard', 'p_cms_pages', 'p_cms_menus', 'p_cms_filemanager');
		foreach($qualificationArray as $qualifier)
			if(isset($options[$qualifier])) 
				$this->db->set($qualifier, $options[$qualifier]);
	
		$this->db->where('g_id', $options['g_id']);	
		$this->db->update('groups');	
		return $this->db->affected_rows();
	}
	
	function get($options = array())
	{
		$options = $this->_default(array('sort_direction' => 'asc'), $options);
	
		$qualificationArray = array('g_id', 'g_name', 'g_description', 'p_cms_dashboard', 'p_cms_pages', 'p_cms_menus', 'p_cms_filemanager');
		foreach($qualificationArray as $qualifier)
			if(isset($options[$qualifier])) 
				$this->db->where($qualifier, $options[$qualifier]);
	
		if(isset($options['limit']) && isset($options['offset'])) 
			$this->db->limit($options['limit'], $options['offset']);
		else if(isset($options['limit'])) 
			$this->db->limit($options['limit']);
	
		if(isset($options['sort_by'])) 
		$this->db->order_by($options['sort_by'], $options['sort_direction']);
		
		$query = $this->db->get('groups');
		
		if($query->num_rows() == 0) 
			return false;
	
		if(isset($options['g_id']))
			return $query->row_array(0);
		else
			return $query->result_array();
	}
	
	function delete($options = array())
	{
		if(!$this->_required(array('g_id'), $options)) 
			return false;
	
		$this->db->where('g_id', $options['g_id']);
		$this->db->delete('groups');
	}

}

?>